/**
 * Created by yuris on 16.12.14.
 */
package view {

import ShopObjects.AbstractShelfView;

import components.ShelfComponent;

import controller.DebugConsoleCommand;

import flash.geom.Point;
import messages.ModelToViewMessage;
import messages.ViewToViewMessage;

import model.Constants;

import model.objects.ProductData;
import model.objects.ShelfData;

import model.objects.ShopObjectData;
import model.prototypes.ShelfProto;

import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.filters.BlurFilter;
import starling.filters.ColorMatrixFilter;

public class ShelfComponentMediator extends AbstractComponentMediator {
    [Inject]
    public var shelfComponent:ShelfComponent;

    private var _shopObjectData:ShopObjectData = null;

    private var _shelfView:AbstractShelfView;




    //---------------register---------------
    override protected function onRegister():void {
        this._shelfView = shelfComponent.view as AbstractShelfView;
        super._createViewAsStaticObject(shelfComponent);
        //---------DEBUG info
        //sendMessage(DebugMessage.CONSOLE_COMMAND, {type:DebugMessage.CONSOLE_SHOW_OBJECT_Z_INDEX, data:shelfComponent.view});
        //---------------------
        this._updateProducts(shelfComponent.model);

        shelfComponent.model.updateSignal.add(this._onDataUpdate);
        _shelfView.addEventListener(Event.REMOVED_FROM_STAGE,this.HANDLER_removedFromStage);

        //shelfComponent.view.touchable = false;

        addHandler(ModelToViewMessage.MODE_CHANGED, HANDLER_modeChanged);

    }

    private function HANDLER_modeChanged(data:Object):void {
        shelfComponent.view.removeEventListener(TouchEvent.TOUCH, HANDLER_touchViewInteriorMode);
        switch (data.mode){
            case Constants.MODE_INTERIOR:
                   // shelfComponent.view.addEventListener(TouchEvent.TOUCH, HANDLER_touchViewInteriorMode);
                (shelfComponent.view as AbstractShelfView).setProductsVisible(false);
                break;
            case Constants.MODE_SIMULATION:
                (shelfComponent.view as AbstractShelfView).setProductsVisible(true);
                break;
        }
    }

    private var _bufTouch:Touch;
    private function HANDLER_touchViewInteriorMode(event:TouchEvent):void {
        _bufTouch = event.getTouch(shelfComponent.view);
        if(_bufTouch){
            if(_highlightFilter2 == null){
                _highlightFilter2 = BlurFilter.createGlow(0x4444ff,10,0.25,0.5);
            }
            this.shelfComponent.view.filter = _highlightFilter2;
        } else {
            this.shelfComponent.view.filter = null;
        }

    }




    private function HANDLER_removedFromStage(event:Event):void {
        mediatorMap.unmediate(shelfComponent);
    }
    //---------------remove---------------
    override protected function onRemove():void {
        trace("on remove shelf mediator");

        shelfComponent.model.updateSignal.remove(this._onDataUpdate);
        _shelfView.removeEventListener(Event.REMOVED_FROM_STAGE,this.HANDLER_removedFromStage);

        removeAllHandlers();
    }



    private static var _highlightFilter1:BlurFilter = null;
    private static var _highlightFilter2:BlurFilter = null;
    private static var _highlightFilter2a:ColorMatrixFilter = null;
    private var _bufProductData:ProductData;
    //---------------update-----------------
    protected function _onDataUpdate(commandName:String, param:Object):void {
        //-------------
        //TODO: do update according to commandName and param
        //--------------

        switch (commandName){
            case Constants.OBJECT_POSITION_UPDATE_SIGNAL:
                this.shelfComponent.view.position = param as Point;
                break;
            case Constants.OBJECT_SHELF_SET_PRODUCT_SIGNAL:
                this._shelfProtoBuf = shelfComponent.model.proto as ShelfProto;
                this._bufProductData = param.product as ProductData;
                (shelfComponent.view as AbstractShelfView).setProductOn(param.shelf_part,this._bufProductData.proto.spriteId,100*(this._bufProductData.count*this._bufProductData.proto.volume)/this._shelfProtoBuf.partVolume);
                break;
            case Constants.OBJECT_SHELF_DELETE_PRODUCT_SIGNAL:
                this._bufProductData = param.product as ProductData;
                (shelfComponent.view as AbstractShelfView).setProductOn(param.shelf_part,null,0);
                break;
            case Constants.OBJECT_TOGGLE_HIGHLIGHT_SIGNAL:
                //this.shelfComponent.view.alpha = (Boolean(param) == true)?0.5:1;
                if(_highlightFilter1 == null){
                    _highlightFilter1 = BlurFilter.createGlow(0xffffff,10,0.25,0.5);
                }
                if(_highlightFilter2 == null){
                    //_highlightFilter2 = BlurFilter.createGlow(0x2244ff,10,0.55,0.7);
                    _highlightFilter2a = new ColorMatrixFilter();
                    _highlightFilter2a.adjustBrightness(0.2);

                }

                    switch (int(param)){
                        case 0:
                            this.shelfComponent.view.filter = null;
                            this.shelfComponent.view.alpha = 1
                            break;
                        case 1:
                            this.shelfComponent.view.filter = _highlightFilter2a;

                            break;
                        case 2:
                            this.shelfComponent.view.filter = _highlightFilter2a;
                            //this.shelfComponent.view.alpha = 0.5;
                            break;
                    }
                //this.shelfComponent.view.parent.addChild(this.shelfComponent.view)
                break;
            case Constants.OBJECT_ANGLE_UPDATE_SIGNAL:
                shelfComponent.view.angle = int(param);
                break;
            case Constants.OBJECT_DESTOY_SIGNAL:
                sendMessage(ViewToViewMessage.REQUEST_UNMEDIATE, {object:shelfComponent});
                shelfComponent.gameLayer.removeSortableObject(shelfComponent.view);
                //shelfComponent.view.removeFromParent(true);
                break;
        }

        trace("this._onDataUpdate");
    }

    private var _shelfProtoBuf:ShelfProto;
    private function _updateProducts(shopObjectData:ShopObjectData):void {
            _shelfView.clear();

            this._shelfProtoBuf = shopObjectData.proto as ShelfProto;

            for(var i:int =0;i<this._shelfProtoBuf.partsNum; i++){
                this._bufProductData = (shopObjectData as ShelfData).products[i];
                if(this._bufProductData != null){
                    (shelfComponent.view as AbstractShelfView).setProductOn(i,this._bufProductData.proto.spriteId,100*(this._bufProductData.count*this._bufProductData.proto.volume)/this._shelfProtoBuf.partVolume);
                }
            }
    }



}
}
